package com.jxc.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import com.jxc.mapper.UserMapper;
import com.jxc.po.User;
import com.jxc.util.DBUtil;
import java.net.URLDecoder;


@Controller
public class ModifyPWController  {
	  @RequestMapping(value="/modifypw")
	  //public void login(PrintWriter out) {  
	  public void login(String userid,String oldpwd,String newpwd,PrintWriter out) {  
		SqlSession sqlSession = null;
		User rs=new User();
		JSONObject jsonobject =null;	
		try {
			sqlSession = DBUtil.getSession();
			UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
			rs.setUserid(userid);
			rs.setPwd(oldpwd);
			User nu=userMapper.select(rs);
			Map map = new HashMap();      
			if(nu==null){				  
				 map.put( "result", 1);       				 
				 jsonobject = JSONObject.fromObject( map ); 
			}else{
				 map.put( "result", 0);       				 
				 jsonobject = JSONObject.fromObject( map ); 
				 rs.setPwd(newpwd);
				 userMapper.updatepwd(rs);
			}
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
		} finally {
			DBUtil.closeSession(sqlSession);
		}
		System.out.println(jsonobject);
		out.print(jsonobject);
	  }  
	
}
